package org.goj.user.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.goj.common.entity.user.Role;
import org.goj.common.model.Result;
import org.goj.user.service.ResourceService;
import org.goj.user.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 权限授予 前端控制器
 * </p>
 *
 * @author CK
 * @since 2020-12-16
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private ResourceService resourceService;

    /**
     * 查询角色
     *
     * @param current 当前页
     * @param size    页面大小
     * @return Result<Role>
     * @author CK
     * @date 2021/5/9 18:41
     */
    @GetMapping("/listRole")
    public Result<Page<Role>> listRole(
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Role> page = new Page<>(current, size);
        Page<Role> page1 = roleService.page(page);
        Assert.notNull(page1, "查询角色失败!");
        return Result.succeed("查询角色成功", page1);
    }


    /**
     * 添加角色
     *
     * @param roleName    角色名称
     * @param defunct     状态
     * @param resourceIds 资源ID集合
     * @param roleId      角色ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/9 19:47
     */
    @PostMapping("/insertRole")
    public Result<Boolean> insertRole(
            @RequestParam(value = "roleName") String roleName,
            @RequestParam(value = "defunct") String defunct,
            @RequestParam(value = "resourceIds") List<Integer> resourceIds,
            @RequestParam(value = "roleId", required = false, defaultValue = "-1") Integer roleId) {
        boolean flag = roleService.insertRole(roleId, roleName, defunct, resourceIds);
        Assert.isTrue(flag, roleId == -1 ? "添加角色失败" : "修改角色失败");
        //刷新Redis存储角色映射
        resourceService.initResourceRolesMap();
        return Result.succeed(roleId == -1 ? "添加角色成功" : "添加角色失败", true);
    }

    /**
     * 查询角色详情
     *
     * @param roleId 角色ID
     * @return Result<Role>
     * @author CK
     * @date 2021/5/10 15:54
     */
    @GetMapping("/roleById")
    public Result<Role> roleById(@RequestParam(value = "roleId") Integer roleId) {
        Role role = roleService.roleById(roleId);
        Assert.notNull(role, "查询角色失败");
        return Result.succeed("查询角色成功", role);
    }

    /**
     * 修改角色状态
     *
     * @param roleId  角色ID
     * @param defunct 状态
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/10 18:24
     */
    @PostMapping("/updateDefunct")
    public Result<Boolean> updateDefunct(
            @RequestParam(value = "roleId") Integer roleId,
            @RequestParam(value = "defunct") String defunct) {
        Role role = new Role();
        role.setDefunct("N".equals(defunct) ? "Y" : "N").setRoleId(roleId);
        boolean b = roleService.updateById(role);
        Assert.isTrue(b, "更新状态失败");
        return Result.succeed("更新状态成功", true);
    }

    /**
     * 删除角色及其资源映射
     *
     * @param roleId 角色ID
     * @return Result<Boolean>
     * @author CK
     * @date 2021/5/10 18:40
     */
    @PostMapping("/deleteRole")
    public Result<Boolean> deleteRole(@RequestParam(value = "roleId") Integer roleId) {
        boolean flag = roleService.deleteRole(roleId);
        Assert.isTrue(flag, "删除角色失败");
        return Result.succeed("删除角色成功", true);
    }

}

